package com.estronger.shopmanage.ui.aftermarket.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.aftermarket.bean.MediaGridBean;
import com.estronger.shopmanage.utils.VideoUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.soonfor.evaluate.activity.ShowPicActivity;

import java.util.ArrayList;
import java.util.List;

public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaHolder>{


    List<MediaGridBean> gridBeanList = new ArrayList<>();


    VideoUtils videoUtils;

    Context mContext;
    public MediaAdapter(Context context){
        this.mContext = context;
        videoUtils = new VideoUtils(context);
    }

    public List<MediaGridBean> getGridBeanList() {
        return gridBeanList;
    }

    @NonNull
    @Override
    public MediaHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (viewType == 0){
            View view = inflater.inflate(R.layout.media_title_layout,parent,false);
            return new  MediaHolder(view,0);
        }else {
            View view = inflater.inflate(R.layout.media_image_layout,parent,false);
            return new  MediaHolder(view,1);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull MediaHolder holder, int position) {
        final MediaGridBean bean = gridBeanList.get(position);
        if (bean.getViewType() == MediaGridBean.ViewType.TIEL){
            holder.titleTextView.setText(bean.getName());
        }else {
            final String url = NetManager.getDownloadURL()
                    + "?filepath=" + bean.getUrl();
            if (bean.getfType().equals("0")){
                MyApplication.imageLoader.displayImage(url, holder.imageView,
                        MyApplication.options);
            }else {
                videoUtils.showVideoThumbImgeToImgeView(url,holder.imageView);
            }
            final int fP = position;
            final ImageView fImageView = holder.imageView;
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(url);
                    if (bean.getfType().equals("0")){
                        ShowPicActivity.previewCanDelete((Activity)mContext, list, fP, false, 1000, false);
                    }else {
                        videoUtils.showVideo(url,fImageView);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return gridBeanList.size();
    }

    @Override
    public int getItemViewType(int position) {
        MediaGridBean bean = gridBeanList.get(position);
        return bean.getViewType() == MediaGridBean.ViewType.TIEL?0:1;
    }

    class MediaHolder extends RecyclerView.ViewHolder{

        TextView titleTextView;
        ImageView imageView;

        public MediaHolder(View itemView,int type) {
            super(itemView);
            if (type == 0){
                titleTextView = itemView.findViewById(R.id.media_title);
            }else {
                imageView = itemView.findViewById(R.id.media_img);
            }
        }
    }
}
